import { _decorator, Component, Node, v3, clamp01, } from 'cc';
import { GlobalConfig } from '../../../Init/Config/GlobalConfig';
import GlobalPool from '../../../Init/Tools/GlobalPool';
import { MappingObj } from './MappingObj';
const { ccclass, property } = _decorator;

@ccclass('MappingMine')
export class MappingMine extends MappingObj {
    isReadyBoom = false;
    boomDelay = GlobalConfig.Mine.boomDelay;
    curt = 0;
    coundownArea: Node = null;

    init(): void {
        //位置
        this.tmpP.set(this.mappingProps.node.position);
        this.tmpP.z = -this.tmpP.y;
        this.tmpP.y = 0;
        this.node.setPosition(this.tmpP.multiplyScalar(this.s3d));
        //旋转
        this.tmpR.x = 0;
        this.tmpR.z = 0;
        if (Math.abs(this.mappingProps.node.eulerAngles.x) == 180) {
            this.tmpR.y = -(this.mappingProps.node.eulerAngles.z % 360) + 180;
        } else {
            this.tmpR.y = this.mappingProps.node.eulerAngles.z;
        }
        this.node.eulerAngles = this.tmpR;
        //缩放
        const s = GlobalConfig.Scale2DTo3D * GlobalConfig.Mine.atkRadius * 2;
        this.node.setScale(v3(s, s, s));
        //
        this.coundownArea = this.node.getChildByName('countdown');
        this.coundownArea.setScale(0, 0, 0);
    }

    update(dt: any): void {
        this.checkState(dt);
    }

    checkState(dt) {
        if (!this.mappingProps.node.active) {
            GlobalPool.put(this.node);
        }

        if (!this.isReadyBoom) {
            this.isReadyBoom = this.mappingProps.data.isReadyBoom;
            this.curt = 0;
        }

        if (this.isReadyBoom) {
            this.curt += dt;
            if (this.curt <= this.boomDelay) {
                let s = clamp01(this.curt / this.boomDelay);
                s = Math.sin(s * 1.57) * 1.25;
                this.coundownArea.setScale(s, s, 1);
            }
        }
    }

}

